<?php
class ControllerApiCustomer extends Controller
{
    public function index()
    {
        $this->load->language('api/customer');

        // Delete past customer in case there is an error
        unset($this->session->data['customer']);

        $json = array();

        if (!isset($this->session->data['api_id'])) {
            $json['error']['warning'] = $this->language->get('error_permission');
        } else {
            // Add keys for missing post vars
            $keys = array(
                'customer_id',
                'customer_group_id',
                'firstname',
                'email',
                'telephone',
                'fax'
            );

            foreach ($keys as $key) {
                if (!isset($this->request->post[$key])) {
                    $this->request->post[$key] = '';
                }
            }

            // Customer
            if ($this->request->post['customer_id']) {
                $this->load->model('account/customer');

                $customer_info = $this->model_account_customer->getCustomer($this->request->post['customer_id']);

                
        
                if (!$customer_info || !$this->customer->login($this->request->post['customer_id'], '', true)) {
                    $json['error']['warning'] = $this->language->get('error_customer');
                }
            }

            if (!(isset($this->request->post['email']) && $this->request->post['email']) && !(isset($this->request->post['telephone']) && $this->request->post['telephone'])) {
                $json['error']['email'] = $this->language->get('error_email_and_telephone_cannot_all_null');
                $json['error']['telephone'] = $this->language->get('error_email_and_telephone_cannot_all_null');
            }

            if ((isset($this->request->post['email']) && $this->request->post['email'])) {
                if ((isset($this->request->post['email']) && $this->request->post['email'])) {
                    if ((utf8_strlen($this->request->post['email']) > 96) || (!filter_var($this->request->post['email'], FILTER_VALIDATE_EMAIL))) {
                        $json['error']['email'] = $this->language->get('error_email');
                    }
                }
            }

            if ((isset($this->request->post['telephone']) && $this->request->post['telephone'])) {
                if ((utf8_strlen($this->request->post['telephone']) < 3) || (utf8_strlen($this->request->post['telephone']) > 32)) {
                    $json['error']['telephone'] = $this->language->get('error_telephone');
                }
            }
        
      
















            // Customer Group
            if (is_array($this->config->get('config_customer_group_display')) && in_array($this->request->post['customer_group_id'], $this->config->get('config_customer_group_display'))) {
                $customer_group_id = $this->request->post['customer_group_id'];
            } else {
                $customer_group_id = $this->config->get('config_customer_group_id');
            }

            // Custom field validation
            $this->load->model('account/custom_field');

            $custom_fields = $this->model_account_custom_field->getCustomFields($customer_group_id);

            foreach ($custom_fields as $custom_field) {
                if (($custom_field['location'] == 'account') && $custom_field['required'] && empty($this->request->post['custom_field'][$custom_field['custom_field_id']])) {
                    $json['error']['custom_field' . $custom_field['custom_field_id']] = sprintf($this->language->get('error_custom_field'), $custom_field['name']);
                } elseif (($custom_field['location'] == 'account') && ($custom_field['type'] == 'text') && !empty($custom_field['validation']) && !filter_var($this->request->post['custom_field'][$custom_field['custom_field_id']], FILTER_VALIDATE_REGEXP, array('options' => array('regexp' => $custom_field['validation'])))) {
                    $json['error']['custom_field' . $custom_field['custom_field_id']] = sprintf($this->language->get('error_custom_field'), $custom_field['name']);
                }
            }

            if (!$json) {
                $this->session->data['customer'] = array(
                    'customer_id'       => $this->request->post['customer_id'],
                    'customer_group_id' => $customer_group_id,
                    'firstname'         => $this->request->post['firstname'],
                    'email'             => $this->request->post['email'],
                    'telephone'         => $this->request->post['telephone'],
                    'fax'               => $this->request->post['fax'],
                    'custom_field'      => isset($this->request->post['custom_field']) ? $this->request->post['custom_field'] : array()
                );

                $json['success'] = $this->language->get('text_success');
            }
        }

        if (isset($this->request->server['HTTP_ORIGIN'])) {
            $this->response->addHeader('Access-Control-Allow-Origin: ' . $this->request->server['HTTP_ORIGIN']);
            $this->response->addHeader('Access-Control-Allow-Methods: GET, PUT, POST, DELETE, OPTIONS');
            $this->response->addHeader('Access-Control-Max-Age: 1000');
            $this->response->addHeader('Access-Control-Allow-Headers: Content-Type, Authorization, X-Requested-With');
        }

        $this->response->addHeader('Content-Type: application/json');
        $this->response->setOutput(json_encode($json));
    }
}
